package com.sugon.controller.module;

import com.sugon.annotation.AutoLog;
import com.sugon.dao.ModuleSeriesTypeDao;
import com.sugon.entity.ModuleSeriesTypeEntity;
import com.sugon.service.ModuleSeriesTypeService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 模具系列类型Controller
 *
 * @author sugon
 * @date 2019-08-19 10:42:29
 */
@Controller
@RequestMapping("moduleseriestype")
public class ModuleSeriesTypeController {

    @Autowired
    private ModuleSeriesTypeService moduleSeriesTypeService;
    @Autowired
    private ModuleSeriesTypeDao moduleSeriesTypeDao;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("moduleseriestype:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ModuleSeriesTypeEntity> moduleSeriesTypeList = moduleSeriesTypeService.queryList(query);
        int total = moduleSeriesTypeService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(moduleSeriesTypeList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("moduleseriestype:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ModuleSeriesTypeEntity moduleSeriesType = moduleSeriesTypeService.queryObject(id);

        return R.ok().put("moduleSeriesType", moduleSeriesType);
    }

    /**
     * 保存
     */
    @AutoLog("模具系列类型--保存")
    @RequestMapping("/save")
    @RequiresPermissions("moduleseriestype:save")
    @ResponseBody
    public R save(@RequestBody ModuleSeriesTypeEntity moduleSeriesType) {
        ModuleSeriesTypeEntity moduleSeriesTypeInDB = moduleSeriesTypeDao.loadByCode(moduleSeriesType.getSeriesCode());
        if (moduleSeriesTypeInDB != null) {
            return R.error("模具系列编号已存在,请确认");
        }
        return moduleSeriesTypeService.save(moduleSeriesType) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @AutoLog("模具系列类型--修改")
    @RequestMapping("/update")
    @RequiresPermissions("moduleseriestype:update")
    @ResponseBody
    public R update(@RequestBody ModuleSeriesTypeEntity moduleSeriesType) {
        moduleSeriesTypeService.update(moduleSeriesType);

        return R.ok();
    }

    /**
     * 删除
     */
    @AutoLog("模具系列类型--删除")
    @RequestMapping("/delete")
    @RequiresPermissions("moduleseriestype:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        moduleSeriesTypeService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ModuleSeriesTypeEntity> list = moduleSeriesTypeService.queryList(params);

        return R.ok().put("list", list);
    }
}
